﻿
using CERT.Services;
using CERT.Services.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CERT.Controllers
{
    using System.Data.Entity.Infrastructure;
    using System.Linq;
    using System.Web.Http;
    using System.Web.Mvc;
    // using Filters;
    // using Models;


    // [Authorize]
    public class AdminController : ApiController
    {
        private INETServices Service;


        public AdminController()
        {
            Service = new NETServices();

        }



        //[HttpGet, ActionName("GetIcon")]
        //public ActionResult GetIcon(int id)
        //{
        //    byte[] photo = null; //Service.GetSingleLookup(a => a.LookupID == id).Icon;
        //    return File(photo, "image/jpeg");

        //}

        //[HttpPost]
        //public ActionResult FileUpload(string qqfile)
        //{
        //    var path = @"C:\\Temp\\100\\";
        //    var file = string.Empty;

        //    try
        //    {
        //        var stream = Request.InputStream;
        //        if (String.IsNullOrEmpty(Request["qqfile"]))
        //        {
        //            // IE
        //            HttpPostedFileBase postedFile = Request.Files[0];
        //            stream = postedFile.InputStream;
        //            // file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName));
        //        }
        //        else
        //        {
        //            //Webkit, Mozilla
        //            // file = Path.Combine(path, qqfile);
        //        }

        //        var buffer = new byte[stream.Length];
        //        stream.Read(buffer, 0, buffer.Length);
        //        System.IO.File.WriteAllBytes(file, buffer);
        //    }
        //    catch (Exception ex)
        //    {
        //        return Json(new { success = false, message = ex.Message }, "application/json");
        //    }

        // GET api/<controller>/List/
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public IEnumerable<Lookup> GetByDesc(string desc)
        {

            var x = Service.GetAllLookups(a => a.Discriminator == desc);
            return x.AsEnumerable<Lookup>();
        }

        // GET api/<controller>/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public Lookup Get(int id)
        {
            Lookup Lookups = Service.GetSingleLookup(a => a.LookupID == id);

            if (Lookups == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return Lookups;
        }

        // POST api/<controller>
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize(Roles = "admin")]
        public HttpResponseMessage Post([FromBody]Lookup value)
        {

            //todo add validation check to determine if lookuptypeid is passed
            //if not get one.
            if (ModelState.IsValid)
            {
                Service.AddLookup(value);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, value);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = value.LookupID }));
                return response;

            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // PUT api/<controller>/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize(Roles = "admin")]
        public HttpResponseMessage Put(int id, [FromBody]Lookup value)
        {

            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != value.LookupID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            try
            {

                Service.UpdateLookup(value);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // DELETE api/<controller>/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize(Roles="admin")]
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                Service.DeleteLookup(id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}
